import Pages from '../../pages/pages.vue'
import ImportMoney from '../import-money/import-money.vue'
import { showMsg, searchUser, searchTotal, setMenuType } from '../../../vuex/action.js'

export default {
  name: 'Users',
  components: {
    'vue-pages': Pages,
    'vue-import-money': ImportMoney
  },
  data() {
    return {
      totalBalance: 0,
      personNum: 0,
      searchType: 1,
      searchTxt: '',
      sortType: '',
      users: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.moneyUserListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xSearchUser: searchUser,
      xSearchTotal: searchTotal,
      xSetMenuType: setMenuType
    }
  },
  ready() {
    this.xSetMenuType('money')
    this.searchUser({})
    this.xSearchTotal()
      .then((totalBalance) => {
        this.totalBalance = totalBalance
      })
  },
  methods: {
    searchUser({start=this.pages.start, limit=this.pages.limit, txt=this.searchTxt, sort=this.sortType}) {
      var data = {
        start: start,
        limit: limit
      }
      if (txt.length) {
        switch (+this.searchType) {
          case 1:
            data.phone = txt
            break
          case 2:
            data.id = txt
            break
          case 3:
            data.nick = txt
            break
          default:
        }
      }
      if (sort.length) {
        data.sort = sort
      }
      this.xSearchUser(data)
        .then((res) => {
          this.personNum = res.currentBalanceOwnerCount
          this.users = res.list
          this.pages.totalPages = Math.ceil(res.totalCount / limit)
        })
    },
    sort() {
      switch (this.sortType) {
        case '':
          this.sortType = 'balanceAsc'
          break
        case 'balanceAsc':
          this.sortType = 'balanceDesc'
          break
        case 'balanceDesc':
          this.sortType = ''
          break
        default:
      }
      this.searchUser(this.pages.start, this.pages.limit, this.searchTxt, this.sortType)
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.searchUser({})
      }
    }
  }
}
